package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.louwowo.cache.key.RedisKeys;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

@RestController
public class LoginController {
    @Reference
    private IUserInfoService userInfoService;
    //验证手机号码是否已经注册过
    @RequestMapping("checkPhone")
    public boolean checkPhone(String phone){
        return !userInfoService.checkPhone(phone);
    }
    //发送验证码
    @RequestMapping("sendVerifyCode")
    public AjaxResult sendVerifyCode(String phone){
            userInfoService.sendVerifyCode(phone);
        return new AjaxResult();
    }
    //用户注册
    @RequestMapping("userRegist")
    public AjaxResult userRegist(String phone,String nickname,String password,String rpassword,String verifyCode){
        userInfoService.regist(phone,nickname,password,rpassword,verifyCode);
        return new AjaxResult();
    }
    //用户登陆
    @RequestMapping("userLogin")
    public AjaxResult userLogin(String username, String password, HttpServletResponse resp){
        String token = userInfoService.userLogin(username, password);
        //把token存到cookie中
        CookieUtil.addCookie("token",token,RedisKeys.USERINFO_LOGIN.getTime().intValue(),resp);
        return new AjaxResult();
    }
}
